package com.zy.servlet.car;

import com.zy.service.impl.DeleteFromCarServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateCarQuantity")
public class DeleteFromCarServlet extends HttpServlet {
    private DeleteFromCarServiceImpl deleteFromCarService = new DeleteFromCarServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer userId = (Integer) req.getSession().getAttribute("userId");
        String shopIdParam = req.getParameter("shopId");
        String changeParam = req.getParameter("change");

        if (userId == null || shopIdParam == null || changeParam == null) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid parameters");
            return;
        }

        int shopId = Integer.parseInt(shopIdParam);
        int change = Integer.parseInt(changeParam);

        boolean success;
        if (change < 0) {
            success = deleteFromCarService.decreaseQuantity(userId, shopId);
        } else {
            // Handle increase quantity logic if needed
            success = true; // Placeholder for increase logic
        }

        if (success) {
            resp.setStatus(HttpServletResponse.SC_OK);
        } else {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Failed to update quantity");
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
